
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ability_env.h"
#include "ability_manager.h"
#include "securec.h"
#include "ability_slice.h"

#include "screensaver_ability_slice.h"
#include "ui_config.h"
#include "file_server.h"
#include "week_date.h"

using namespace std;

namespace OHOS {
REGISTER_AS(ScreenSaverAbilitySlice)

ScreenSaverAbilitySlice::ScreenSaverAbilitySlice()
{
    Task::Init();
    SetPeriod(TASK_TIME_PERIOD);
}

ScreenSaverAbilitySlice::~ScreenSaverAbilitySlice()
{
    if (background_ != nullptr) {
        delete background_;
        background_ = nullptr;
    }

    if (clockIcon_ != nullptr) {
        delete clockIcon_;
        clockIcon_ = nullptr;
    }

    if (weatherIcon_ != nullptr) {
        delete weatherIcon_;
        weatherIcon_ = nullptr;
    }

    if (timeLabel_ != nullptr) {
        delete timeLabel_;
        timeLabel_ = nullptr;
    }

    if (dateLabel_ != nullptr) {
        delete dateLabel_;
        dateLabel_ = nullptr;
    }

    if (weatherLabel_ != nullptr) {
        delete weatherLabel_;
        weatherLabel_ = nullptr;
    }

    if (pm25Label_ != nullptr) {
        delete pm25Label_;
        pm25Label_ = nullptr;
    }

    if (pm25QatLabel_ != nullptr) {
        delete pm25QatLabel_;
        pm25QatLabel_ = nullptr;
    }

    if (dialogWin_ != nullptr) {
        delete dialogWin_;
        dialogWin_ = nullptr;
    }

    if (onClick_ != nullptr) {
        delete onClick_;
        onClick_ = nullptr;
    }
}

void ScreenSaverAbilitySlice::SetWeatherInfo()
{
    weatherLabel_ = new UILabel();
    weatherLabel_->SetPosition(WEATHER_X_POS, WEATHER_Y_POS, WEATHER_WIDTH, WEATHER_HEIGHT);
    weatherLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    weatherLabel_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    weatherLabel_->SetFont(FONT_NAME, FONT_SIZE_24);
    weatherLabel_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
    weatherLabel_->SetText((char *)"多云 | 24℃");

    pm25Label_ = new UILabel();
    pm25Label_->SetPosition(PM25_X_POS, PM25_Y_POS, PM25_WIDTH, PM25_HEIGHT);
    pm25Label_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    pm25Label_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    pm25Label_->SetFont(FONT_NAME, FONT_SIZE_24);
    pm25Label_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
    pm25Label_->SetText((char *)"PM2.5");

    pm25QatLabel_ = new UILabel();
    pm25QatLabel_->SetPosition(PM25_QAT_X, PM25_QAT_Y, PM25_QAT_W, PM25_QAT_H);
    pm25QatLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    pm25QatLabel_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    pm25QatLabel_->SetFont(FONT_NAME, FONT_SIZE_18);
    pm25QatLabel_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
    pm25QatLabel_->SetText((char *)"良好");
}

void ScreenSaverAbilitySlice::SetTimeDate()
{
    timeLabel_ = new UILabel();
    timeLabel_->SetPosition(TIME_X_POS, TIME_Y_POS, TIME_WIDTH, TIME_HEIGHT);
    timeLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
    timeLabel_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    timeLabel_->SetFont(FONT_NAME, FONT_SIZE_56);
    timeLabel_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));

    dateLabel_ = new UILabel();
    dateLabel_->SetPosition(DATE_X_POS, DATE_Y_POS, DATE_WIDTH, DATE_HEIGHT);
    dateLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    dateLabel_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    dateLabel_->SetFont(FONT_NAME, FONT_SIZE_28);
    dateLabel_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
}

static void GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size)
{
    const char *weekday;
    if (date == nullptr) {
        return;
    }
    weekday = WeekDate::GetWeekByYear(iY, iM, iD, WeekDate::LANGUAGE_CHI);
    strncpy_s(date, size, weekday, strlen(weekday));
}

void ScreenSaverAbilitySlice::UpdateTimeDate()
{
    char hour_min[TMP_BUF_SIZE] = {0};
    char month_day[TMP_BUF_SIZE] = {0};
    char week_day[TMP_BUF_SIZE] = {0};
    char date[TMP_BUF_SIZE] = {0};

    const int16_t january = 1;
    const int16_t commonYear = 1900;
    time_t t = time(nullptr);
    struct tm *st = localtime(&t);

    if (st->tm_min == minute_) {
        return;
    }

    minute_ = st->tm_min;
    (void)sprintf_s(hour_min, sizeof(hour_min), "%02d:%02d", st->tm_hour, st->tm_min);
    (void)sprintf_s(month_day, sizeof(month_day), "%02d月:%02d日", st->tm_mon + january, st->tm_mday);
    GetWeekdayByYearday(st->tm_year + commonYear, st->tm_mon + january, st->tm_mday, week_day, sizeof(week_day));
    (void)sprintf_s(date, sizeof(date), "%s %s", month_day, week_day);

    timeLabel_->SetText((char *)hour_min);
    dateLabel_->SetText((char *)date);
}

void ScreenSaverAbilitySlice::Callback()
{
    if (isActivity_ == false) {
        return;
    }

    if (quit_) {
        AbilitySlice *nextSlice;
        Want wantData = {nullptr};

        nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName((char *)"HoodsAbilitySlice");
        if (nextSlice == nullptr) {
            printf("ERROR! undefined HoodsAbilitySlice! \n");
        } else {
            Present(*nextSlice, wantData);
            return;
        }
        quit_ = false;
    }
    UpdateTimeDate();

    timeLabel_->Invalidate();
    dateLabel_->Invalidate();
}

int ScreenSaverAbilitySlice::DialogDisplay(const char *msg, int type, int timeout)
{
    int result = 0;
    int tmCnt = 0;

    dialogWin_->Display(msg, type, timeout);

    while (true) {
        result = dialogWin_->GetResult();
        if (result != -1) {
            break;
        }
        sleep(1);

        if (timeout > 0 && ++tmCnt >= timeout) {
            result = 0;
            break;
        }
    }

    quit_ = true;

    return result;
}

static int ShowDialog(void *data, const char *msg, DIALOG_TYPE type, int timeout)
{
    ScreenSaverAbilitySlice *saver = (ScreenSaverAbilitySlice *)data;
    if (saver == nullptr) {
        return 0;
    }

    return saver->DialogDisplay(msg, type, timeout);
}

void ScreenSaverAbilitySlice::OnStart(const Want &want)
{
    AbilitySlice::OnStart(want);

    char iconPath[256] = {0};
    const char *pathHead = GetSrcPath();

    rootView_ = RootView::GetWindowRootView();
    rootView_->SetPosition(START_XPOS, START_YPOS, SCREEN_WIDTH, SCREEN_HEIGHT);
    rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);

    (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead, SCRSAV_BACKGROUND);
    background_ = new UIImageView();
    background_->SetPosition(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    background_->SetSrc((char *)iconPath);
    background_->SetStyle(STYLE_BACKGROUND_OPA, 0);
    background_->SetTouchable(true);
    onClick_ = new HoodsButtonClickListener(this);
    onClick_->SetSliceInfo((const char *)"HoodsAbilitySlice", nullptr);
    background_->SetOnClickListener(onClick_);

    memset(iconPath, 0x00, sizeof(iconPath));
    (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead, SCRSAV_CLOCK_ICON);
    clockIcon_ = new UIImageView();
    clockIcon_->SetPosition(CLOCK_X_POS, CLOCK_Y_POS, CLOCK_WIDTH, CLOCK_HEIGHT);
    clockIcon_->SetSrc((char *)iconPath);
    clockIcon_->SetStyle(STYLE_BACKGROUND_OPA, 0);

    memset(iconPath, 0x00, sizeof(iconPath));
    (void)sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pathHead, SCRSAV_WEATHER_ICON);
    weatherIcon_ = new UIImageView();
    weatherIcon_->SetPosition(WEA_ICON_X, WEA_ICON_Y, WEA_ICON_W, WEA_ICON_H);
    weatherIcon_->SetSrc((char *)iconPath);
    weatherIcon_->SetStyle(STYLE_BACKGROUND_OPA, 0);

    SetTimeDate();
    SetWeatherInfo();

    rootView_->Add(background_);
    rootView_->Add(clockIcon_);
    rootView_->Add(weatherIcon_);
    rootView_->Add(timeLabel_);
    rootView_->Add(dateLabel_);
    rootView_->Add(weatherLabel_);
    rootView_->Add(pm25Label_);
    rootView_->Add(pm25QatLabel_);

    dialogWin_ = new PopupDialog();
    dialogWin_->Create(rootView_, POP_WIN_X, POP_WIN_Y, POP_WIN_W, POP_WIN_H);

    SetUIContent(rootView_);
    rootView_->Invalidate();
    TaskExecute();
}

void ScreenSaverAbilitySlice::OnInactive()
{
    AbilitySlice::OnInactive();
}

void ScreenSaverAbilitySlice::OnActive(const Want &want)
{
    AbilitySlice::OnActive(want);
    isActivity_ = true;
    quit_ = false;
    FileServerSetDialog(ShowDialog, this);
}

void ScreenSaverAbilitySlice::OnBackground()
{
    AbilitySlice::OnBackground();
    isActivity_ = false;
}

void ScreenSaverAbilitySlice::OnStop()
{
    AbilitySlice::OnStop();
}
}
